package org.thingsboard.server.controller;

import com.google.common.util.concurrent.ListenableFuture;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thingsboard.server.common.data.ApiUsageState;
import org.thingsboard.server.common.data.kv.Aggregation;
import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery;
import org.thingsboard.server.common.data.kv.ReadTsKvQueryResult;
import org.thingsboard.server.dao.timeseries.TimeseriesService;
import org.thingsboard.server.dao.usagerecord.ApiUsageStateService;

import java.util.List;

@RestController
@RequestMapping("/api/test")
@RequiredArgsConstructor
@Slf4j
public class TestController extends BaseController{

    private final TimeseriesService timeseriesService;
    private final ApiUsageStateService apiUsageStateService;

    @GetMapping("/ts")
    public List<ReadTsKvQueryResult> testTs() throws Exception {
        ApiUsageState apiUsageState = apiUsageStateService.findTenantApiUsageState(getTenantId());

        BaseReadTsKvQuery ruleEngineExecutionCount = new BaseReadTsKvQuery("ruleEngineExecutionCountHourly", 1683412873609L,
                1686004873609L, 86400000, 30, Aggregation.SUM);

        ListenableFuture<List<ReadTsKvQueryResult>> future = timeseriesService.findAllByQueries(apiUsageState.getTenantId(), apiUsageState.getId(), List.of(ruleEngineExecutionCount));
        return future.get();
    }
}
